package cn.dapeng.thread;

import cn.dapeng.util.SleepingUtil;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.thread
 * @Description TODO
 * @ClassName ThreadCommunicationT04
 * @Author xupenghong
 * @Date 2024/8/28 14:00
 * @Version 1.0.0
 */
public class ThreadCommunicationT05 {

    enum ReadToRun {
        T1, T2;
    }

    static volatile ReadToRun readToRun = ReadToRun.T1;

    public static void main(String[] args) {
        char[] aI = "1234567".toCharArray();
        char[] bI = "ABCDEFG".toCharArray();

        new Thread(() -> {
            for (char c : aI) {
                //自旋的方式，效率低，因为一直占有CPU
                while (readToRun != ReadToRun.T1) {
                }
                System.out.print(c);
                readToRun = ReadToRun.T2;
            }
        }).start();

        new Thread(() -> {
            for (char c : bI) {
                while (readToRun != ReadToRun.T2) {

                }
                System.out.print(c);
                readToRun = ReadToRun.T1;
            }
        }).start();


    }

}
